package cn.orangeframe.neon.infrastructure.repository.mapper;

import cn.orangeframe.neon.domain.entity.WorkflowDef;
import cn.orangeframe.neon.domain.entity.GraphData;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface MetadataMapper {

    @Select("select json_data from meta_workflow_def")
    List<WorkflowDef> getAllWorkflowDef();

    @Select("select max(version) from meta_workflow_def where name = #{name}")
    Integer getLatestVersion(@Param("name") String name);

    @Select("select json_data from meta_workflow_def where name = #{name} and version = #{version}")
    WorkflowDef getWorkflowDef(@Param("name") String name, @Param("version") int version);

    @Insert("insert into meta_workflow_def (name, description, version, latest_version, json_data) values " +
            "(#{def.name}, #{def.description}, #{def.version}, #{latest_version}, #{json_data})")
    Integer registerWorkflowDef(WorkflowDef def, int latest_version, String json_data);

    @Update("update meta_workflow_def set json_data = #{json_data}, description = #{def.description} where name = #{def.name} and version = #{def.version}")
    Integer updateWorkflowDef(WorkflowDef def, int latest_version, String json_data);

    @Select("select json_data from graph_workflow_def")
    List<GraphData> getAllGraphWorkflowDef();

    @Select("select max(version) from graph_workflow_def where name = #{name}")
    Integer getGraphLatestVersion(@Param("name") String name);

    @Select("select json_data from graph_workflow_def where name = #{name} and version = #{version}")
    GraphData getGraphWorkflowDef(@Param("name") String name, @Param("version") int version);

    @Insert("insert into graph_workflow_def (name, description, version, latest_version, json_data) values " +
            "(#{def.name}, #{def.description}, #{def.version}, #{latest_version}, #{json_data})")
    Integer registerGraphWorkflowDef(GraphData def, int latest_version, String json_data);

    @Update("update meta_workflow_def set json_data = #{json_data}, description = #{def.description} where name = #{def.name} and version = #{def.version}")
    Integer updateGraphWorkflowDef(GraphData def, int latest_version, String json_data);
}
